#include <stdio.h>
#include <math.h>
#include <time.h>

double f1(int n, double a[], double x);
double f2(int n, double a[], double x);

#define MAXN 10 // 多项式最大项数
#define BECHMARK 10000 // 函数执行的次数 

clock_t start, stop;
double duration;

int main(int argc, const char *argv[]) {

    double a[MAXN];
    for (int i=0; i<MAXN; i++) {
        a[i] = (double)i;
    }

    start = clock();
    for (int i=0; i<BECHMARK; i++) {
        f1(MAXN, a, 1.1);
    }
    stop = clock();
    duration = ((double)(stop-start)) / CLOCKS_PER_SEC;
    printf("%s%.6f\n", "Function 1 Duration: ", duration);

    start = clock();
    for (int i=0; i<BECHMARK; i++) {
        f2(MAXN, a, 1.1);
    }
    stop = clock();
    duration = ((double)(stop-start)) / CLOCKS_PER_SEC;
    printf("%s%.6f\n", "Function 2 Duration: ", duration);

    return 0;
}

double f1(int n, double a[], double x) {
    int i;
    double p = a[0];
    for (i=1; i<=n; i++) {
        p += (a[i] * pow(x, i));
    }
    return p;
}

double f2(int n, double a[], double x) {
    int i;
    double p = a[n];
    for (i=n; i>0; i--) {
        p = a[i-1] + x*p;
    }
    return p;
}
